﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormStore.实体库;
using WinFormStore.数据层;
using static WinFormStore.FrmMain;

namespace WinFormStore.Erp
{
    public partial class FrmGoodView : Form
    {
        //配置文件路径
        public string path;
        //配置服务对象
        public ScannerInfoService infoService;
        //配置实体对象
        public ScannerInfo scannerInfo;
        public ScannerComService comService =null;//声明数据层对象

        public FrmGoodView()
        {
            InitializeComponent();
        }

        public FrmGoodView(string _path, ScannerInfoService _infoService, ScannerInfo _scannerInfo)
        {

            InitializeComponent();
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            this.path = _path;
            this.infoService = _infoService;
            this.scannerInfo = _scannerInfo;
           // this.FormClosing += new FormClosingEventHandler(FrmGoodView_FormClosing);
        }
        private List<string> codeList = new List<string>();//所有条码集合
        private void BindType()
        {
            DataTable dt = new DataTable();
            DBHelper.PrepareSql("select * from [Type]");
            dt = DBHelper.ExecuteQuery();
            DataRow dr = dt.NewRow();
            dr["TypeName"] = "--请选择--";
            dr["TypeId"] = "0";
            dt.Rows.InsertAt(dr, 0);
            this.cmbType.DataSource = dt;
            this.cmbType.DisplayMember = "TypeName";
            this.cmbType.ValueMember = "TypeID";
        }
        private void BindData()
        {
            string sql = "select * from Goods inner join [Type] on Goods.TypeID=[Type].TypeID where 1=1";
            if (!this.txtBarCode.Text.Equals(""))
            {
                sql += "and BarCode ='" + this.txtBarCode.Text.Trim() + "'";
            }
            if (!this.txtGoodsName.Text.Equals(""))
            {
                sql += "and GoodsName like '%" + this.txtGoodsName.Text.Trim() + "%'";
            }
            if (!this.cmbTimeSelect.Text.Equals("全部"))
            {
                sql += string.Format("and StockDate between '{0}' and '{1}'"
                    , this.dtpStartTime.Value.ToString("yyyy-MM-dd") + " 00:00:00"
                    , this.dtpEndTime.Value.ToString("yyyy-MM-dd") + " 23:59:59");
            }
            if (!this.cmbType.SelectedValue.ToString().Equals("0"))
            {
                sql += "and Goods.TypeID=" + this.cmbType.SelectedValue.ToString();

            }
            DBHelper.PrepareSql(sql);
            DataTable dt = new DataTable();
            dt = DBHelper.ExecuteQuery();
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.DataSource = dt;
            this.lblRowCount.Text = "当前共" + dt.Rows.Count + "条记录！";
        }
        private void SelectBycode()
        {
            this.Invoke(new EventHandler(delegate
            {
                string sql = "select * from Goods inner join [Type] on Goods.TypeID=[Type].TypeID where BarCode=@BarCode";
                DBHelper.PrepareSql(sql);
                DBHelper.SetParameter("BarCode", this.txtBarCode.Text);
                DataTable dt = new DataTable();
                dt = DBHelper.ExecuteQuery();
                this.dataGridView1.AutoGenerateColumns = false;
                this.dataGridView1.DataSource = dt;
                this.lblRowCount.Text = "当前共" + dt.Rows.Count + "条记录！";
            }));
        }
        private void ComService_CodeReceived(string code)
        {
            //空码判断
            if (code.Length == 0)
            {
                AddLog("条形码为空");
                return;
            }
            //已存在判断
            if (codeList.Contains(code))
            {
                this.Invoke(new EventHandler(delegate
                {

                }));

            }
            //如果有需要，可以添加其他特殊判断...
            //正确读取显示
            codeList.Add(code);
            AddLog(code);
            SelectBycode();
        }
        private void AddLog(string message)
        {
            if (this.txtBarCode.InvokeRequired)
            {
                this.txtBarCode.Invoke(new Action<string>(AddLog), message);
            }
            else
            {
                this.txtBarCode.Text = message;
            }
        }
        private void FrmGoodView_Load(object sender, EventArgs e)
        {
            comService = new ScannerComService();//实例化数据层对象
            comService.Connect(scannerInfo);
            comService.CodeReceived += ComService_CodeReceived;
            BindType();
            this.dtpStartTime.Enabled = false;
            this.dtpEndTime.Enabled = false;
            BindData();
        }

        private void cmbTimeSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cmbTimeSelect.Text.Equals("全部"))
            {
                this.dtpStartTime.Enabled = false;
                this.dtpEndTime.Enabled = false;
            }
            else if (this.cmbTimeSelect.Text.Equals("本年"))
            {
                this.dtpStartTime.Enabled = true;
                this.dtpEndTime.Enabled = true;
                DateTime now = DateTime.Now;
                int dayOfYear = now.DayOfYear;
                this.dtpStartTime.Value = now.AddDays(-dayOfYear + 1);
                this.dtpEndTime.Value = now.AddDays(-dayOfYear + 1).AddYears(1).AddDays(-1);
            }
            else if (this.cmbTimeSelect.Text.Equals("本月"))
            {
                this.dtpStartTime.Enabled = true;
                this.dtpEndTime.Enabled = true;
                DateTime now = DateTime.Now;
                int dayOfMonth = now.Day;
                this.dtpStartTime.Value = now.AddDays(-dayOfMonth + 1);
                this.dtpEndTime.Value = now.AddDays(-dayOfMonth + 1).AddMonths(1).AddDays(-1);
            }
            else if (this.cmbTimeSelect.Text.Equals("本周"))
            {
                this.dtpStartTime.Enabled = true;
                this.dtpEndTime.Enabled = true;
                DateTime now = DateTime.Now;
                int dayOfWeek = (int)now.DayOfWeek;
                if (dayOfWeek == 0)
                {
                    dayOfWeek = 7;
                }
                this.dtpStartTime.Value = now.AddDays(-dayOfWeek + 1);
                this.dtpEndTime.Value = now.AddDays(7 - dayOfWeek);
            }
            else if (this.cmbTimeSelect.Text.Equals("本日"))
            {
                this.dtpStartTime.Enabled = true;
                this.dtpEndTime.Enabled = true;
                DateTime now = DateTime.Now;
                this.dtpStartTime.Value = now;
                this.dtpEndTime.Value = now;
            }

        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            BindData();
        }



        private void FrmGoodView_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.comService?.DisConn();
        }
    }
}
